package com.gdit.zax.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.DynamicDao;
import com.gdit.zax.dao.SignDao;
import com.gdit.zax.entitys.Dynamic;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;

public class SignDaoImpl implements SignDao {

	private PreparedStatement ps;
	private ResultSet rs;
	
	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	
	public SignDaoImpl(Connection conn,HttpServletRequest request, HttpServletResponse response) {
		this.conn = conn;
		this.request = request;
		this.response = response;
	}
	
	/**
	 * 签到
	 */
	public boolean signin(int uid) {
		try {
			String sql = "select * from t_dynamic where UserId=? and DynType=2 and to_days(AddTime) = to_days(now())";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			
			rs = ps.executeQuery();
			if (!rs.next()){
				DynamicDao ddao =  DAOFactory.init(conn, request, response).getDynamicDao();
				return ddao.addSystemDynamic(Constant.DYNAMICSYS_signin, uid, -1, "签到", Constant.PIC_signin);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return false;
	}
	
	public boolean delsign(int sid) {
		String sql = "delete from t_dynamic where Id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, sid);
			
			int result = ps.executeUpdate();
			if (result > 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public ArrayList<Dynamic> listsign() {
		ArrayList<Dynamic>  list = new ArrayList<Dynamic>();
		try {
			String sql = "select * from t_dynamic where DynType=2";
			ps = conn.prepareStatement(sql);
			
			rs = ps.executeQuery();
			while (rs.next()){
				Dynamic dy = DBUtil.getobject(rs, Dynamic.class);
				list.add(dy);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public ArrayList<Dynamic> listsign(int uid) {
		ArrayList<Dynamic>  list = new ArrayList<Dynamic>();
		try {
			String sql = "select * from t_dynamic where UserId=? and DynType=2";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			
			rs = ps.executeQuery();
			while (rs.next()){
				Dynamic dy = DBUtil.getobject(rs, Dynamic.class);
				list.add(dy);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public ArrayList<Dynamic> listsign(int page, int onepagecount) {
		
		return null;
	}

	public ArrayList<Dynamic> listsign(int uid, int page, int onepagecount) {
		return null;
	}

	public ArrayList<Dynamic> listsignbytime(int uid, int year, int month) {
		ArrayList<Dynamic>  list = new ArrayList<Dynamic>();
		try {
			String sql = "select * from t_dynamic where UserId=? and DynType=2 and year(AddTime)=? and month(AddTime)=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, year);
			ps.setInt(3, month);
			
			rs = ps.executeQuery();
			while (rs.next()){
				Dynamic dy = DBUtil.getobject(rs, Dynamic.class);
				list.add(dy);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public int getSignSum(int uid) {
		String sql = "select count(Id) from t_dynamic where UserId=? and DynType=2";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			
			rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}


}
